package com.fh.config;

import com.fh.common.util.ConvertUtils;
import com.fh.common.util.ReflectUtils;
import com.fh.modules.ucas.entity.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.binding.MapperMethod.ParamMap;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * @author Qiu,Junda
 * @date 2019年4月3日
 * @description mybatis拦截器，自动注入创建人、创建时间、修改人、修改时间
 */
@Slf4j
@Component
@Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) })
public class MybatisInterceptor implements Interceptor {

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
		String sqlId = mappedStatement.getId();
		SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
		Object parameter = invocation.getArgs()[1];
		if (parameter == null) {
			return invocation.proceed();
		}
		if (SqlCommandType.INSERT == sqlCommandType) {
			List<Field> fields= ReflectUtils.getClassAllFields(parameter);
			for (Field field : fields) {
                String fieldName=field.getName();
				try {
					if("delFlag".equals(fieldName)){
						field.setAccessible(true);
						field.set(parameter,0);
						field.setAccessible(false);
					}
					else if ("createBy".equals(fieldName)) {
						field.setAccessible(true);
						Object localCreateBy = field.get(parameter);
						field.setAccessible(false);
						if (ConvertUtils.isEmpty(localCreateBy)) {
							String createBy = "";
							// 获取登录用户信息
							//TODO
							SysUser sysUser =null;
//							SysUser sysUser = (SysUser) ShiroUtils.getShiroPrincipal();
							if (sysUser != null) {
								// 登录账号
								createBy = sysUser.getUsername();
							}
							if (!ConvertUtils.isEmpty(createBy)) {
								field.setAccessible(true);
								field.set(parameter, createBy);
								field.setAccessible(false);
							}
						}
					}
					// 注入创建时间
					else if ("createTime".equals(fieldName)) {
						field.setAccessible(true);
						Object localCreateDate = field.get(parameter);
						field.setAccessible(false);
						if (ConvertUtils.isEmpty(localCreateDate)) {
							field.setAccessible(true);
							field.set(parameter, new Date());
							field.setAccessible(false);
						}
					}
				} catch (Exception e) {
				}
			}
		}
		if (SqlCommandType.UPDATE == sqlCommandType&&parameter!=null) {

			if (parameter instanceof ParamMap) {
				ParamMap<?> p = (ParamMap<?>) parameter;
				parameter = p.get("param1");
			}
            List<Field> fields= ReflectUtils.getClassAllFields(parameter);
			for (Field field : fields) {
			    String fieldName=field.getName();
				try {
					if ("updateBy".equals(fieldName)) {
						field.setAccessible(true);
						Object localUpdateBy = field.get(parameter);
						field.setAccessible(false);
						if (ConvertUtils.isEmpty(localUpdateBy)) {
							String updateBy = "";
							// 获取登录用户信息
							//TODO
							SysUser sysUser =null;
//							SysUser sysUser = (SysUser) ShiroUtils.getShiroPrincipal();
							if (sysUser != null) {
								// 登录账号
								updateBy = sysUser.getUsername();
							}
							if (!ConvertUtils.isEmpty(updateBy)) {
								field.setAccessible(true);
								field.set(parameter, updateBy);
								field.setAccessible(false);
							}
						}
					}
					else if ("updateTime".equals(fieldName)) {
						field.setAccessible(true);
						Object localUpdateDate = field.get(parameter);
						field.setAccessible(false);
						if (ConvertUtils.isEmpty(localUpdateDate)) {
							field.setAccessible(true);
							field.set(parameter, new Date());
							field.setAccessible(false);
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
					log.error(e.getMessage(),e);
				}
			}
		}
		return invocation.proceed();
	}

	@Override
	public Object plugin(Object target) {
		return Plugin.wrap(target, this);
	}

	@Override
	public void setProperties(Properties properties) {
	}

}
